/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.helper;

import java.util.ArrayList;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.TemplateBinding;
import org.eclipse.uml2.uml.TemplateableElement;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.GraphEdgeHelper;
import com.cea.papyrus.core.helper.IGraphEdgeHelper;
import com.cea.papyrus.core.helper.NodeHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Factory that can create and delete a {@link org.eclipse.uml2.uml.Usage}.
 * 
 * @author Patrick Tessier
 */
public class EdgeTemplateBindingHelper extends GraphEdgeHelper implements IGraphEdgeHelper{
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */
	/**
	 * 
	 * 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateGraphElement(GraphElement source){
		
		Element eltSource=null;
//		if (true)
//			return false ;
		if ((source instanceof Diagram)  ){
			if (((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)((Diagram)source).getOwner()).getElement();
				if (eltSource instanceof Package){return true;}
			}
		}
		if (source.getSemanticModel()!=null ){
			if (((Uml1SemanticModelBridge)source.getSemanticModel()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)source.getSemanticModel()).getElement();
				if (eltSource instanceof  org.eclipse.uml2.uml.Package){
					return true;
				}

			}
		}

		return false;
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#createGraphEdge(org.eclipse.uml2.uml.Element, com.cea.papyrus.diagraminterchange2.di2.Diagram)
	 */
	/**
	 * 
	 * 
	 * @param umlElement 
	 * @param diagram 
	 * 
	 * @return 
	 */
	@Override
	public ArrayList createGraphEdge(Element umlElement, Diagram diagram){
		Debug.debug(this,"createGraphEdge", 10);
		GraphEdge ge=(GraphEdge)this.createGraphElement(umlElement, null, null, null);
		//look for UML connected UML element
		if (!(umlElement instanceof Dependency)){
			Debug.error(this, "EdgeTemplateBindingFactory.createGraphEdge: "+ umlElement+ "is not a TemplateBinding!", channel);
		}
		Dependency de=(Dependency)umlElement;

		Element umlsupplier=(Element)de.getSuppliers().get(0);
		Element umlsource=(Element)de.getSources().get(0);
		super.endCreateGraphElement(umlsource, umlsupplier, ge, diagram);
		return new ArrayList();
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.NodeFactory#createGraphNodeWithSemantic(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlChild 
	 * @param size 
	 * @param location 
	 * @param parent 
	 * 
	 * @return 
	 */
	@Override
	public GraphElement createGraphElement(Element umlChild, GraphElement parent, Point location, Dimension size) {

		GraphElement graphElement=super.createGraphElement(umlChild, null, location, size);
		NodeHelper nf=new NodeHelper();
//		name of the dependency
		GraphElement graphNode=nf.createGraphElement(null, graphElement, new Point(0, 10), null);
		graphNode.setFontSize(9);
		graphNode.setIsVisible(false);

//		stereotype of dependency
		graphNode=nf.createGraphElement(null, graphElement, new Point(0, 30), null);
		graphNode.setFontSize(9);

		//the parent has to be connected in last, because a event will be triggered.
		if(parent!=null){
			parent.getContained().add(graphElement);
		}

		return graphElement;	
	}

	/////////////////////////////////////////////////////////////////////////////////
	//                       UMLManagement
	/////////////////////////////////////////////////////////////////////////////////
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * @param name 
	 * 
	 * @return 
	 */
	@Override
	public  org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"create association", channel); //$NON-NLS-1$
		TemplateBinding binding= UMLFactory.eINSTANCE.createTemplateBinding();
		((TemplateableElement)umlParent).getTemplateBindings().add(binding) ;
//		as.setName(name);
//		if (umlParent instanceof Package){
//		((Package)umlParent).getPackagedElements().add(as);
//		}
//		if (umlParent instanceof CollaborationUse){
//			((CollaborationUse)umlParent).getRoleBindings().add(as);
//		}
		return binding;		
	}


	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#canCreateUMLElement(org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean canCreateUMLElement(Element umlParent) {
		if(umlParent instanceof TemplateableElement) {
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#addUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean addUMLElement(Element element, Element umlParent) {
		return ((TemplateableElement)umlParent).getTemplateBindings().add((TemplateBinding)element) ;
//		if(umlParent instanceof Package) {
//			return ((Package)umlParent).getPackagedElements().add(element);
//		}
//		if (umlParent instanceof CollaborationUse){
//			return ((CollaborationUse)umlParent).getRoleBindings().add(element);
//		}
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#removeUMLElement(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param element 
	 * @param umlParent 
	 * 
	 * @return 
	 */
	@Override
	public boolean removeUMLElement(Element element, Element umlParent) {
//		if(umlParent instanceof Package) {
//			return ((Package)umlParent).getPackagedElements().remove(element);
//		}
//		if (umlParent instanceof CollaborationUse){
//			return ((CollaborationUse)umlParent).getRoleBindings().remove(element);
//		}
//		return false;
		return ((TemplateableElement)umlParent).getTemplateBindings().remove(element) ;
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#getBaseString()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	public String getBaseString() {
		return "TemplateBinding"; //$NON-NLS-1$
	}
	
	/////////////////////////////////////////////////////////////////////////////////
	//                       LINK MANAGEMENT
	/////////////////////////////////////////////////////////////////////////////////
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#connect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean connect(Element source, Element target, Element link) {
		TemplateBinding binding = (TemplateBinding)link ;
		binding.setBoundElement((TemplateableElement)source) ;
		binding.setSignature(((TemplateableElement)target).getOwnedTemplateSignature()) ;
//		Dependency dependency=(Dependency)link;
//		dependency.setName("From" + ((NamedElement)source).getName()+ "to"+((NamedElement)target).getName());
//		dependency.getSuppliers().clear();
//		dependency.getSuppliers().add(target);
//		//		target
//		dependency.getClients().clear();
//		dependency.getClients().add(source);
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.GraphEdgeFactory#deconnect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * 
	 * 
	 * @param target 
	 * @param link 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean deconnect(Element source, Element target, Element link){
		TemplateBinding binding = (TemplateBinding)link ;
		binding.setBoundElement(null) ;
		binding.setSignature(null) ;
//		Dependency dependency=(Dependency)link;
//		dependency.setName(getNewUMLElementName(link.getOwner()));
//		dependency.getSuppliers().remove(target);
//		//		target
//		dependency.getClients().remove(source);
		return true;
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.factories.IGraphEdgeFactory#canExecute(com.cea.papyrus.diagraminterchange2.di2.GraphNode, com.cea.papyrus.diagraminterchange2.di2.GraphNode)
	 */	
	/**
	 * 
	 * 
	 * @param target 
	 * @param source 
	 * 
	 * @return 
	 */
	@Override
	public boolean canConnect(Element source,Element target){
		// if no source or no target found
		if ((source == null) && (target == null)) {
			return false;
		}
		if ((source != null) && (target == null)) {
			if(source instanceof TemplateableElement){
				return true;
			}
		}
		// if target is the source
		if (source ==target) {
			return false;
		}
		// Test source and target types
		if ((source instanceof TemplateableElement) && (target instanceof TemplateableElement))	{
			if (((TemplateableElement)target).isTemplate()) {
				EClass sourceClass = source.eClass() ;
				EClass targetClass = target.eClass() ;
				if (sourceClass == targetClass || sourceClass.getEAllSuperTypes().contains(targetClass))
					return true;
			}
		}
		return false;
	}
}
